﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 该类记录了场景中所有带有ItemFlag脚本的物体
/// 生成、销毁、激活、禁用等所有操作都应该通过此脚本
/// </summary>
public class ItemManager : MonoBehaviour {
	[System.Serializable]
	public class ItemCategory
	{
		public string CategoryName{
			get{ return categoryName;}
		}
		string categoryName;
		public List<ItemFlag> itemFlags = new List<ItemFlag> ();
		public ItemFlag this[string index]{
			get{ 
				foreach (ItemFlag iFlag in itemFlags) {
					if (iFlag.ItemName == index) {
						return iFlag;
					}
				}
				return null;
			}
		}
		public ItemCategory(string setCategoryName){
			categoryName = setCategoryName;
		}
		public void Add(ItemFlag newFlag){
			if (this [newFlag.ItemName]) {
				Debug.Log ("The ItemFlag named " + newFlag.ItemName + " is existed in ItemManager.itemList!");
				//Add (newFlag, 1);
				return;
			}
			itemFlags.Add (newFlag);
		}
		//void Add(ItemFlag newFlag, int num){
		//	string newName = newFlag.ItemName + "_" + num.ToString ();
		//	if (this [newName]) {
		//		Add (newFlag, num++);
		//	} else {
		//		newFlag.ItemName = newName;
		//		itemFlags.Add (newFlag);
		//	}
		//}
	}
	///场景中所有带有ItemFlag脚本的分类列表
	public List<ItemCategory> itemList;
	///一开始被禁用的物品，需要手动拖拽
	public List<ItemFlag> disableItems;
	/// 游戏中所有可能动态生成的预设体
	public GameObject[] AllObj { get { return allObj; } }
	[SerializeField] GameObject[] allObj;
	const string itemPath = "Prefab/";

	public static ItemManager ItemMan;
	public static ItemManager _instance;

	/// <summary>
	/// 根据类名获取该类物品列表
	/// </summary>
	/// <param name="index">Index.</param>
	public List<ItemFlag> this[string index]{
		get{ 
			ItemCategory ic = ContainsCategory (GetCategoryOfItemName (index));
			if (ic == null) {
				Debug.Log ("Category " + index + " isn't existed!");
				return null;
			}
			return ic.itemFlags;
		}
	}

	void Awake(){
		allObj = Resources.LoadAll<GameObject> (itemPath);
		ItemManager._instance = ItemManager.ItemMan = this;
		initItemList ();
	}
	/// <summary>
	/// 初始化列表
	/// </summary>
	void initItemList(){
		ItemFlag[] itemFlags = GameObject.FindObjectsOfType<ItemFlag> ();
		foreach (ItemFlag f in itemFlags) {
			addFlagInList (f);
		}
		foreach (ItemFlag f in disableItems) {
			addFlagInList (f);
		}
		disableItems.Clear ();
	}
	/// <summary>
	/// 将物品标记添加进列表中
	/// </summary>
	/// <param name="f">F.</param>
	public void addFlagInList(ItemFlag f){
		string[] nameDiv = f.ItemName.Split ('_');
		if (nameDiv.Length >= 2) {
			ItemCategory ic = ContainsCategory (nameDiv [0]);
			if (ic == null) {
				ic = new ItemCategory (nameDiv [0]);
				itemList.Add (ic);
			}
			ic.Add (f);
		}
		else if (nameDiv.Length == 1 && nameDiv[0] != "") {
			ItemCategory ic = ContainsCategory ("");
			if (ic == null) {
				ic = new ItemCategory ("");
				itemList.Add (ic);
			}
			ic.Add (f);
		}
	}

	/// <summary>
	/// 判断列表中是否包含该类
	/// 如果包含则返回该类列表，否则返回null
	/// </summary>
	/// <returns>The category.</returns>
	/// <param name="categoryName">Category name.</param>
	ItemCategory ContainsCategory(string categoryName){
		foreach (ItemCategory ic in itemList) {
			if (ic.CategoryName == categoryName) {
				return ic;
			}
		}
		//当categoryName==""时，返回null表示other类还没有建立
		return null;
	}
	/// 获取ItemName的类名
	string GetCategoryOfItemName(string itemName){
		string[] nameDiv = itemName.Split ('_');
		if (nameDiv.Length >= 2) {
			return nameDiv [0];
		}
		return "";
	}



	/// <summary>
	/// 根据名字获取表中的ItemFlag
	/// </summary>
	/// <returns>The item flag.</returns>
	/// <param name="flagName">物品名</param>
	public ItemFlag getItemFlag(string flagName){
		List<ItemFlag> ifs = this [flagName];
		foreach (ItemFlag f in ifs) {
			if (f.ItemName == flagName) {
				return f;
			}
		}
		Debug.LogError ("The ItemFlag named " + flagName + " isn't existed!");
		return null;
	}
	/// <summary>
	/// 根据名字获取表中的ItemFlag
	/// </summary>
	/// <returns>The item flag.</returns>
	/// <param name="flagName">物品名</param>
	public static ItemFlag GetItemFlag(string flagName){
		return ItemManager.ItemMan.getItemFlag (flagName);
	}


	/// <summary>
	/// 生成物品的同时把物品信息记录入表
	/// </summary>
	/// <returns>The item.</returns>
	/// <param name="itemPrefab">Item prefab.</param>
	/// <param name="instPos">Inst position.</param>
	/// <param name="instQuat">Inst quat.</param>
	public static GameObject InstantiateItem(GameObject itemPrefab, Vector3 instPos, Quaternion instQuat){
		ItemManager itemMan = ItemManager.ItemMan;
		GameObject instObj = GameObject.Instantiate (itemPrefab, instPos, instQuat) as GameObject;
		ItemFlag[] ifs = instObj.GetComponentsInChildren<ItemFlag> ();
		foreach (ItemFlag f in ifs) {
			itemMan.addFlagInList (f);
		}
		return instObj;
	}
	public static GameObject InstantiateItem(GameObject itemPrefab, Vector3 instPos){
		return InstantiateItem (itemPrefab, instPos, itemPrefab.transform.rotation);
	}
	public static GameObject InstantiateItem(GameObject itemPrefab){
		return InstantiateItem (itemPrefab, itemPrefab.transform.position, itemPrefab.transform.rotation);
	}

	/// <summary>
	/// 销毁物品同时销毁相应信息
	/// </summary>
	/// <param name="itemName">Item name.</param>
	public static void DestroyItem(string itemName){
		ItemFlag iFlag = ItemManager.GetItemFlag (itemName);
		GameObject destroyTarget = iFlag.gameObject;
		ItemManager.ItemMan [itemName].Remove (iFlag);
		Destroy (destroyTarget);
	}
		
	/// <summary>
	/// 根据Item名使能或禁用场景中的某物体
	/// </summary>
	/// <returns>The item.</returns>
	/// <param name="itemName">Item name.</param>
	/// <param name="setBool">If set to <c>true</c> set bool.</param>
	public static GameObject EnableItem(string itemName, bool setBool = true){
		ItemFlag it = ItemManager.GetItemFlag (itemName);
		if (it == null) {
			Debug.LogError ("The ItemFlag named " + itemName + " isn't existed");
			return null;
		}
		it.gameObject.SetActive (setBool);
		return it.gameObject;
	}

	/// <summary>
	/// 根据Item名从所有的预设体中获取某预设体
	/// </summary>
	/// <returns>The prefab in all object.</returns>
	/// <param name="itemName">ItemFlag的名字</param>
	public GameObject getPrefabInAllObj(string itemName){
		foreach (GameObject go in allObj) {
			ItemFlag fg = go.GetComponent<ItemFlag> ();
			if (fg == null) {
				Debug.LogError ("The prefab named " + go.name + " doesn't have ItemFlag!");
				return null;
			}
			if (fg.ItemName == itemName) {
				return go;
			}
		}
		return null;
	}
}








/*

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 该类记录了场景中所有带有ItemFlag脚本的物体
/// 生成、销毁、激活、禁用等所有操作都应该通过此脚本
/// </summary>
public class ItemManager : MonoBehaviour {
	public List<ItemFlag> systemItems;
	public List<ItemFlag> taskItems;
	public List<ItemFlag> rangeItems;
	public List<ItemFlag> instPosItems;
	public List<ItemFlag> ctrlItems;
	public List<ItemFlag> otherItems;
	///一开始被禁用的物品，需要手动拖拽
	public List<ItemFlag> disableItems;
	/// 游戏中所有可能动态生成的预设体
	public GameObject[] AllObj { get { return allObj; } }
	[SerializeField] GameObject[] allObj;
	const string itemPath = "Prefab/";

	public static ItemManager ItemMan;
	public static ItemManager _instance;

	public List<ItemFlag> this[string index]{
		get{ 
			string typeName = index.Split ('_') [0];
			if (typeName == "Sys") {
				return systemItems;
			} else if (typeName == "Task") {
				return taskItems;
			} else if (typeName == "Range") {
				return rangeItems;
			} else if (typeName == "Pos") {
				return instPosItems;
			} else if (typeName == "Ctrl") {
				return ctrlItems;
			} else {
				return otherItems;
			}
		}
	}

	void Awake(){
		allObj = Resources.LoadAll<GameObject> (itemPath);
		ItemManager._instance = ItemManager.ItemMan = this;
		initItemList ();
	}
	/// <summary>
	/// 初始化列表
	/// </summary>
	public void initItemList(){
		ItemFlag[] itemFlags = GameObject.FindObjectsOfType<ItemFlag> ();
		foreach (ItemFlag f in itemFlags) {
			addFlagInList (f);
		}
		foreach (ItemFlag f in disableItems) {
			addFlagInList (f);
		}
		disableItems.Clear ();
	}
	void addFlagInList(ItemFlag f){
		switch (f.Type) {
		case ItemType.SYSTEM_MANAGER:
			systemItems.Add (f);
			break;
		case ItemType.TASK_ITEM:
			taskItems.Add (f);
			break;
		case ItemType.RANGE:
			rangeItems.Add (f);
			break;
		case ItemType.INST_POS:
			instPosItems.Add (f);
			break;
		case ItemType.CONTROLABLE:
			ctrlItems.Add (f);
			break;
		case ItemType.OTHER:
			otherItems.Add (f);
			break;
		default:
			break; 
		}
	}

	/// <summary>
	/// 根据名字获取表中的ItemFlag
	/// </summary>
	/// <returns>The item flag.</returns>
	/// <param name="flagName">物品名</param>
	public ItemFlag getItemFlag(string flagName){
		List<ItemFlag> ifs = this [flagName];
		foreach (ItemFlag f in ifs) {
			if (f.ItemName == flagName) {
				return f;
			}
		}
		Debug.LogError ("The ItemFlag named " + flagName + " isn't existed!");
		return null;
	}
	/// <summary>
	/// 根据名字获取表中的ItemFlag
	/// </summary>
	/// <returns>The item flag.</returns>
	/// <param name="flagName">物品名</param>
	public static ItemFlag GetItemFlag(string flagName){
		return ItemManager.ItemMan.getItemFlag (flagName);
	}


	/// <summary>
	/// 生成物品的同时把物品信息记录入表
	/// </summary>
	/// <returns>The item.</returns>
	/// <param name="itemPrefab">Item prefab.</param>
	/// <param name="instPos">Inst position.</param>
	/// <param name="instQuat">Inst quat.</param>
	public static GameObject InstantiateItem(GameObject itemPrefab, Vector3 instPos, Quaternion instQuat){
		ItemManager itemMan = ItemManager.ItemMan;
		GameObject instObj = GameObject.Instantiate (itemPrefab, instPos, instQuat) as GameObject;
		ItemFlag[] ifs = instObj.GetComponentsInChildren<ItemFlag> ();
		foreach (ItemFlag f in ifs) {
			itemMan.addFlagInList (f);
		}
		return instObj;
	}
	public static GameObject InstantiateItem(GameObject itemPrefab, Vector3 instPos){
		return InstantiateItem (itemPrefab, instPos, itemPrefab.transform.rotation);
	}
	public static GameObject InstantiateItem(GameObject itemPrefab){
		return InstantiateItem (itemPrefab, itemPrefab.transform.position, itemPrefab.transform.rotation);
	}

	/// <summary>
	/// 销毁物品同时销毁相应信息
	/// </summary>
	/// <param name="itemName">Item name.</param>
	public static void DestroyItem(string itemName){
		ItemFlag iFlag = ItemManager.GetItemFlag (itemName);
		GameObject destroyTarget = iFlag.gameObject;
		ItemManager.ItemMan [itemName].Remove (iFlag);
		Destroy (destroyTarget);
	}

	/// <summary>
	/// 根据Item名使能或禁用场景中的某物体
	/// </summary>
	/// <returns>The item.</returns>
	/// <param name="itemName">Item name.</param>
	/// <param name="setBool">If set to <c>true</c> set bool.</param>
	public static GameObject EnableItem(string itemName, bool setBool = true){
		ItemFlag it = ItemManager.GetItemFlag (itemName);
		if (it == null) {
			Debug.LogError ("The ItemFlag named " + itemName + " isn't existed");
			return null;
		}
		it.gameObject.SetActive (setBool);
		return it.gameObject;
	}

	/// <summary>
	/// 根据Item名从所有的预设体中获取某预设体
	/// </summary>
	/// <returns>The prefab in all object.</returns>
	/// <param name="itemName">ItemFlag的名字</param>
	public GameObject getPrefabInAllObj(string itemName){
		foreach (GameObject go in allObj) {
			ItemFlag fg = go.GetComponent<ItemFlag> ();
			if (fg == null) {
				Debug.LogError ("The prefab named " + go.name + " doesn't have ItemFlag!");
				return null;
			}
			if (fg.ItemName == itemName) {
				return go;
			}
		}
		return null;
	}
}
*/
